package jt.classic.app.ldrc;

import jt.classic.system.ISystem;
import jt.classic.system.SimpleAction;
import jt.classic.system.database.SystemDataSource;
import jt.classic.system.group.GroupManager;
import jt.classic.system.group.IGroup;
import jt.classic.system.user.IUser;
import jt.classic.system.user.UserManager;
import jt.classic.system.wordbook.XWordbookMain;
import jt.classic.system.wordbook.XWordbookQuery;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import org.limp.basework.console.XSystemConsole;
import org.limp.basework.console.XSystemConsoleRunException;
import org.limp.basework.impl.CommonBean4HSQ;

import org.limp.mine.Controller;
import org.limp.mine.DateTrimmer;
import org.limp.mine.Label;
import org.limp.mine.QueryJack;
import org.limp.mine.QueryJackFactory;
import org.limp.mine.StringTool;

import java.sql.Connection;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 领导日程action
 *
 * @author bizkit
 *
 */
public class LdrcAction extends SimpleAction {
    /**
     * 新建领导日程
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public ActionForward neu(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        Connection conn = null;
        ActionForward forward = null;
        IUser user = ISystem.getSessionUser(request);

        try {
            SystemDataSource ds = SystemDataSource.getInstance();
            conn = ds.getConnection();

            UserManager manager = new UserManager(conn);
            List list = manager.getUserByRating1(user, 2);

            request.setAttribute("list", list);

            XWordbookMain wbmain = XWordbookMain.getInstance();
            List noonlist = wbmain.getWord("noon");
            request.setAttribute("noon", noonlist);

            List mscalelist = wbmain.getWord("minute-scale");
            request.setAttribute("mscale", mscalelist);

            List hscalelist = wbmain.getWord("hour-scale");
            request.setAttribute("hscale", hscalelist);

            forward = mapping.findForward("neu");
        } catch (Exception ex) {
            forward = mapping.findForward("error");
            ISystem.catchException(request, this, ex);
        } finally {
            SystemDataSource.closeConnection(conn);
        }

        return forward;
    }

    /**
     * 新建领导日程
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public ActionForward edit(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        Connection conn = null;
        ActionForward forward = null;

        String id = StringTool.checkString(request.getParameter("id"));
        IUser user = ISystem.getSessionUser(request);

        try {
            SystemDataSource ds = SystemDataSource.getInstance();
            conn = ds.getConnection();

            LdrcManager amanager = new LdrcManager(conn);

            ALdrc ldrc = amanager.getALdrcById(id);

            request.setAttribute("ldrc", ldrc);

            UserManager manager = new UserManager(conn);
            List list = manager.getUserByRating1(user, 1);

            request.setAttribute("list", list);

            XWordbookMain wbmain = XWordbookMain.getInstance();
            List noonlist = wbmain.getWord("noon");
            request.setAttribute("noon", noonlist);

            List mscalelist = wbmain.getWord("minute-scale");
            request.setAttribute("mscale", mscalelist);

            List hscalelist = wbmain.getWord("hour-scale");
            request.setAttribute("hscale", hscalelist);

            forward = mapping.findForward("edit");
        } catch (Exception ex) {
            forward = mapping.findForward("error");
            ISystem.catchException(request, this, ex);
        } finally {
            SystemDataSource.closeConnection(conn);
        }

        return forward;
    }

    /**
     * 查看领导日程
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public ActionForward view(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        Connection conn = null;
        ActionForward forward = null;

        String id = StringTool.checkString(request.getParameter("id"));

        try {
            SystemDataSource ds = SystemDataSource.getInstance();
            conn = ds.getConnection();

            LdrcManager amanager = new LdrcManager(conn);

            ALdrc ldrc = amanager.getALdrcById(id);

            request.setAttribute("ldrc", ldrc);

            XWordbookMain wbmain = XWordbookMain.getInstance();

            XWordbookQuery noonquery = wbmain.getWordbookQuery("noon");
            request.setAttribute("noonquery", noonquery);

            XWordbookQuery minutequery = wbmain.getWordbookQuery("minute-scale");
            request.setAttribute("minutequery", minutequery);

            XWordbookQuery hourquery = wbmain.getWordbookQuery("hour-scale");
            request.setAttribute("hourquery", hourquery);

            forward = mapping.findForward("view");
        } catch (Exception ex) {
            forward = mapping.findForward("error");
            ISystem.catchException(request, this, ex);
        } finally {
            SystemDataSource.closeConnection(conn);
        }

        return forward;
    }

    /**
     * 领导日程列表
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public ActionForward ldlist(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        Connection conn = null;
        ActionForward forward = null;

        String rating = StringTool.checkString(request.getParameter("rating"));

        String url = request.getContextPath() + "/app/ldrc.do?method=query";

        try {
            SystemDataSource ds = SystemDataSource.getInstance();
            conn = ds.getConnection();

            GroupManager manager = new GroupManager(conn);

            IGroup group = GroupManager.getRootGroup("领导日程");

//            String script = manager.getGroup4BranchTreeScript(group, true,
//                    Integer.parseInt(rating), url);
//
//            request.setAttribute("script", script);

            forward = mapping.findForward("ldlist");
        } catch (Exception ex) {
            forward = mapping.findForward("error");
            ISystem.catchException(request, this, ex);
        } finally {
            SystemDataSource.closeConnection(conn);
        }

        return forward;
    }

    /**
     * 保存日程安排
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public ActionForward save(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        Connection conn = null;

        ActionForward forward = null;

        try {
            SystemDataSource ds = SystemDataSource.getInstance();
            conn = ds.getConnection();

            XSystemConsole console = new XSystemConsole();
            console.RUN2SAVE(request, conn);

            conn.commit();

            String url = "/ldrc.do?method=list";

            forward = new ActionForward();
            forward.setPath(url);
            forward.setRedirect(true);
        } catch (XSystemConsoleRunException ex) {
            forward = mapping.findForward("error");
            ISystem.catchException(request, this, ex);
            conn.rollback();
        } finally {
            SystemDataSource.closeConnection(conn);
        }

        return forward;
    }

    /**
     * 修改日程安排
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public ActionForward update(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        Connection conn = null;

        ActionForward forward = null;

        try {
            SystemDataSource ds = SystemDataSource.getInstance();
            conn = ds.getConnection();

            this.manualDelete("APP_LDRC_FBFW", request, conn);
            this.manualDelete("APP_LDRC_YHLD", request, conn);

            XSystemConsole console = new XSystemConsole();
            console.RUN2UPDATE(request, conn);

            this.manualSaveX("APP_LDRC_FBFW", "countFB", request, conn);
            this.manualSaveX("APP_LDRC_YHLD", "countLD", request, conn);

            conn.commit();

            String url = "/ldrc.do?method=list";

            forward = new ActionForward();
            forward.setPath(url);
            forward.setRedirect(true);
        } catch (XSystemConsoleRunException ex) {
            forward = mapping.findForward("error");
            ISystem.catchException(request, this, ex);
            conn.rollback();
        } finally {
            SystemDataSource.closeConnection(conn);
        }

        return forward;
    }

    /**
     * 审核通过
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public ActionForward pass(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        Connection conn = null;

        ActionForward forward = null;

        try {
            SystemDataSource ds = SystemDataSource.getInstance();
            conn = ds.getConnection();

            XSystemConsole console = new XSystemConsole();
            console.RUN2UPDATE(request, conn);

            conn.commit();

            String url = "/ldrc.do?method=list4sh";

            forward = new ActionForward();
            forward.setPath(url);
            forward.setRedirect(true);
        } catch (XSystemConsoleRunException ex) {
            forward = mapping.findForward("error");
            ISystem.catchException(request, this, ex);
            conn.rollback();
        } finally {
            SystemDataSource.closeConnection(conn);
        }

        return forward;
    }

    /**
     * 审核拒绝
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public ActionForward reject(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        Connection conn = null;

        ActionForward forward = null;

        try {
            SystemDataSource ds = SystemDataSource.getInstance();
            conn = ds.getConnection();

            XSystemConsole console = new XSystemConsole();
            console.RUN2UPDATE(request, conn);

            conn.commit();

            String url = "/ldrc.do?method=list4sh";

            forward = new ActionForward();
            forward.setPath(url);
            forward.setRedirect(true);
        } catch (XSystemConsoleRunException ex) {
            forward = mapping.findForward("error");
            ISystem.catchException(request, this, ex);
            conn.rollback();
        } finally {
            SystemDataSource.closeConnection(conn);
        }

        return forward;
    }

    /**
     * 删除日程安排
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public ActionForward delete(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        Connection conn = null;

        ActionForward forward = null;

        try {
            SystemDataSource ds = SystemDataSource.getInstance();
            conn = ds.getConnection();

            XSystemConsole console = new XSystemConsole();
            console.RUN2DELETE(request, conn);

            conn.commit();

            String url = "/ldrc.do?method=list";

            forward = new ActionForward();
            forward.setPath(url);
            forward.setRedirect(true);
        } catch (XSystemConsoleRunException ex) {
            forward = mapping.findForward("error");
            ISystem.catchException(request, this, ex);
            conn.rollback();
        } finally {
            SystemDataSource.closeConnection(conn);
        }

        return forward;
    }

    /**
     * 信息列表
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public ActionForward list(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        Connection conn = null;
        ActionForward forward = null;

        IUser user = ISystem.getSessionUser(request);

        CommonBean4HSQ req = new CommonBean4HSQ(request);

        Map param = req.getResource();

        String pageNO = StringTool.checkString(param.get("pageNO"));

        String qname = StringTool.checkString(param.get("qname"));

        String qdate = StringTool.checkString(request.getParameter("qdate"));

        qdate = DateTrimmer.formatDateForVarchar(qdate);

        Map query = new HashMap();

        String sqlwhere = " 1=1 ";

        if (!ISystem.matchrole(user, "ldms")) {
            sqlwhere += (" AND DJRID = '" + user.id() + "'");
        }

        if (!qname.equals("")) {
            sqlwhere += (" AND CHLD LIKE '%" + qname + "%' ");
        }

        query.put("qname", qname);

        if (!qdate.equals("")) {
            sqlwhere += (" AND DJDATE LIKE '%" + qdate + "%' ");
        }

        query.put("qdate", DateTrimmer.getYMD_S_LBL(qdate));

        request.setAttribute("query", query);

        String sql1 = "SELECT COUNT(APP_LDRC.ID) FROM APP_LDRC WHERE ";

        String sql2 = "SELECT APP_LDRC.* FROM APP_LDRC WHERE ";

        sql1 += sqlwhere;

        sql2 += (sqlwhere + " ORDER BY DJDATE DESC");

        String url = request.getContextPath() + "/app/ldrc.do?method=list";

        try {
            SystemDataSource ds = SystemDataSource.getInstance();
            conn = ds.getConnection();

            QueryJack plus = QueryJackFactory.getInstance().createQueryJack(false);

            // 设置
            plus.setParameter(QueryJackFactory.PAGINATION_SQL1, sql1);
            plus.setParameter(QueryJackFactory.PAGINATION_SQL2, sql2);
            plus.setParameter(QueryJackFactory.DATABASE_CONNECTION, conn);
            plus.setParameter(QueryJackFactory.PAGINATION_LINKSTRING, url);
            plus.setParameter(QueryJackFactory.PAGINATION_COUNT, "20");
            plus.setParameter(QueryJackFactory.PAGINATION_PAGENO, pageNO);

            // plus.setParameter(QueryJackFactory.PAGINATION_UPDATEPAGENO, "");
            List resultList = plus.work();

            // 获得跳转控制
            Controller controller = plus.getController("form1");

            // 设置调转类型
            controller.setType("input");

            // 显示标签
            Label label = plus.getLabel();

            // 设置显示标签类型
            label.setType("href");

            Object[] result = new Object[4];
            result[0] = resultList;
            result[1] = label;
            result[2] = controller;
            result[3] = pageNO;

            request.setAttribute("result", result);

            forward = mapping.findForward("list");
        } catch (Exception ex) {
            forward = mapping.findForward("error");
            ISystem.catchException(request, this, ex);
        } finally {
            SystemDataSource.closeConnection(conn);
        }

        return forward;
    }

    /**
     * 审核领导日程
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public ActionForward list4sh(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        Connection conn = null;
        ActionForward forward = null;

        IUser user = ISystem.getSessionUser(request);

        CommonBean4HSQ req = new CommonBean4HSQ(request);

        Map param = req.getResource();

        String pageNO = StringTool.checkString(param.get("pageNO"));

        String qname = StringTool.checkString(param.get("qname"));

        String qdate = StringTool.checkString(request.getParameter("qdate"));

        qdate = DateTrimmer.formatDateForVarchar(qdate);

        Map query = new HashMap();

        String sqlwhere = " (STATUS = -1 OR STATUS = 0) ";

        if (!qname.equals("")) {
            sqlwhere += (" AND CHLD LIKE '%" + qname + "%' ");
        }

        query.put("qname", qname);

        if (!qdate.equals("")) {
            sqlwhere += (" AND DJDATE LIKE '%" + qdate + "%' ");
        }

        query.put("qdate", DateTrimmer.getYMD_S_LBL(qdate));

        request.setAttribute("query", query);

        if (!ISystem.matchrole(user, "ldms")) {
            sqlwhere += (" AND 1 = -1 ");
        }

        String sql1 = "SELECT COUNT(APP_LDRC.ID) FROM APP_LDRC WHERE " +
            sqlwhere;
        String sql2 = "SELECT APP_LDRC.* FROM APP_LDRC WHERE " + sqlwhere +
            "  ORDER BY DJDATE DESC";

        String url = request.getContextPath() + "/app/ldrc.do?method=list4sh";

        try {
            SystemDataSource ds = SystemDataSource.getInstance();
            conn = ds.getConnection();

            QueryJack plus = QueryJackFactory.getInstance().createQueryJack(false);

            // 设置
            plus.setParameter(QueryJackFactory.PAGINATION_SQL1, sql1);
            plus.setParameter(QueryJackFactory.PAGINATION_SQL2, sql2);
            plus.setParameter(QueryJackFactory.DATABASE_CONNECTION, conn);
            plus.setParameter(QueryJackFactory.PAGINATION_LINKSTRING, url);
            plus.setParameter(QueryJackFactory.PAGINATION_COUNT, "20");
            plus.setParameter(QueryJackFactory.PAGINATION_PAGENO, pageNO);

            // plus.setParameter(QueryJackFactory.PAGINATION_UPDATEPAGENO, "");
            List resultList = plus.work();

            // 获得跳转控制
            Controller controller = plus.getController("form1");

            // 设置调转类型
            controller.setType("input");

            // 显示标签
            Label label = plus.getLabel();

            // 设置显示标签类型
            label.setType("href");

            Object[] result = new Object[4];
            result[0] = resultList;
            result[1] = label;
            result[2] = controller;
            result[3] = pageNO;

            request.setAttribute("result", result);

            forward = mapping.findForward("list4sh");
        } catch (Exception ex) {
            forward = mapping.findForward("error");
            ISystem.catchException(request, this, ex);
        } finally {
            SystemDataSource.closeConnection(conn);
        }

        return forward;
    }

    /**
     * 领导日程查询
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public ActionForward query(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        Connection conn = null;
        ActionForward forward = null;

        IUser user = ISystem.getSessionUser(request);

        CommonBean4HSQ req = new CommonBean4HSQ(request);

        Map param = req.getResource();

        String pageNO = StringTool.checkString(param.get("pageNO"));

        String uid = StringTool.checkString(param.get("uid"));

        String qname = StringTool.checkString(param.get("qname"));

        /*开始时间*/
        String qsdate = StringTool.checkString(request.getParameter("qsdate"));

        /*结束时间*/
        String qedate = StringTool.checkString(request.getParameter("qedate"));

        qsdate = DateTrimmer.formatDateForVarchar(qsdate);

        qedate = DateTrimmer.formatDateForVarchar(qedate);

        Map query = new HashMap();

        String sqlwhere = " STATUS = 1 ";

        query.put("qname", qname);

        if (!qname.equals("")) {
            sqlwhere += (" AND CHLD LIKE '%" + qname + "%' ");
        }

        if (!qsdate.equals("") && !qedate.equals("")) {
            sqlwhere += (" AND ((EDATE <= '" + qedate +
            "235959' AND EDATE >= '" + qsdate + "000000') OR (SDATE <= '" +
            qedate + "235959' AND SDATE >= '" + qsdate +
            "000000') OR (SDATE <= '" + qsdate + "000000' AND EDATE >= '" +
            qedate + "235959')) ");
        } else if (!qsdate.equals("") && qedate.equals("")) {
            sqlwhere += (" AND SDATE >= '" + qsdate + "000000' ");
        } else if (qsdate.equals("") && !qedate.equals("")) {
            sqlwhere += (" AND EDATE <= '" + qedate + "235959' ");
        }

        if (!ISystem.matchrole(user, "ldms")) {
            String lumeid = UserManager.iallgroupflumeid(",");

            sqlwhere += (" AND ID IN (SELECT ID FROM APP_LDRC_FBFW WHERE FBID IN (" +
            lumeid + ") AND FBTYPE = 'g')");
        }

        query.put("qsdate", DateTrimmer.getYMD_LBL(qsdate));

        query.put("qedate", DateTrimmer.getYMD_LBL(qedate));

        request.setAttribute("query", query);

        String sql1 = "SELECT COUNT(ID) FROM APP_LDRC WHERE " + sqlwhere;
        String sql2 = "SELECT * FROM APP_LDRC WHERE " + sqlwhere +
            " ORDER BY DJDATE DESC";

        String url = request.getContextPath() + "/app/ldrc.do?method=list";

        XWordbookMain wbmain = XWordbookMain.getInstance();

        XWordbookQuery noonquery = wbmain.getWordbookQuery("noon");
        request.setAttribute("noonquery", noonquery);

        try {
            SystemDataSource ds = SystemDataSource.getInstance();
            conn = ds.getConnection();

            QueryJack plus = QueryJackFactory.getInstance().createQueryJack(false);

            // 设置
            plus.setParameter(QueryJackFactory.PAGINATION_SQL1, sql1);
            plus.setParameter(QueryJackFactory.PAGINATION_SQL2, sql2);
            plus.setParameter(QueryJackFactory.DATABASE_CONNECTION, conn);
            plus.setParameter(QueryJackFactory.PAGINATION_LINKSTRING, url);
            plus.setParameter(QueryJackFactory.PAGINATION_COUNT, "20");
            plus.setParameter(QueryJackFactory.PAGINATION_PAGENO, pageNO);

            // plus.setParameter(QueryJackFactory.PAGINATION_UPDATEPAGENO, "");
            List resultList = plus.work();

            // 获得跳转控制
            Controller controller = plus.getController("form1");

            // 设置调转类型
            controller.setType("input");

            // 显示标签
            Label label = plus.getLabel();

            // 设置显示标签类型
            label.setType("href");

            Object[] result = new Object[4];
            result[0] = resultList;
            result[1] = label;
            result[2] = controller;
            result[3] = pageNO;

            request.setAttribute("result", result);

            forward = mapping.findForward("querylist");
        } catch (Exception ex) {
            forward = mapping.findForward("error");
            ISystem.catchException(request, this, ex);
        } finally {
            SystemDataSource.closeConnection(conn);
        }

        return forward;
    }
}
